<template>
    <div>
        <div class="cell">
            <input placeholder="请输入新的手机号码" v-model="phone"
                   type="number"
                   pattern="[0-9]*"
                   onkeypress='return( /[\d]/.test(String.fromCharCode(event.keyCode) ) )'
                   @blur="isPhone"
            >
        </div>
        <div class="cell">
            <input placeholder="输入验证码"
                   v-model="code"
                   type="number"
                   pattern="[0-9]*"
                   onkeypress='return( /[\d]/.test(String.fromCharCode(event.keyCode) ) )'
            />
            <div class="send" @click="send">{{codetxt}}</div>
        </div>
        <div class="sure" @click="toRevise">完成</div>
    </div>
</template>

<script>
    import API from '../../request/api'
    import {Toast} from 'vant'
    import regular from '@/common/regular';

    export default {
        data() {
            return {
                phone: '',
                code: '',
                codetxt: '发送验证码', // 验证码校验文本
                stateSendCode: true
            };
        },
        computed: {},
        watch: {
            // 监听具体数据变化，并做相应的处理
        },
        methods: {
            isPhone() {
                if (!regular.REGEX_CHINA_MOBILE.test(this.phone)) {
                    Toast ('手机号不正确');
                    this.phone = ''
                } else {
                    this.request.get(API.polyCheckPhone, {phone: this.phone}).then((res) => {
                        if (!res) {
                            //不存在帐号
                            Toast ('可以使用');

                        } else {
                            //存在帐号
                            Toast ('该手机号已注册');
                            this.phone = ''
                        }
                    });
                }
            },
            toRevise() {
                if (this.phone === '') {
                    Toast('请先输入新的手机号');
                } else if (this.code === '') {
                    Toast('请先输入验证吗');
                } else if (this.code.length !== 6) {
                    Toast('验证码请输入6位');
                    this.code = '';
                } else {
                    let data = {
                        phone: this.phone,
                        code: this.code,
                    };
                    this.request.post(API.changePolyPhone, data).then(res => {
                        Toast('修改成功');
                        this.$router.push('poly-account');
                    }, (res) => {
                        console.log(res);
                    });
                }
            },
            send(){
                if (this.phone === '') {
                    Toast('请先输入新的手机号');
                } else if (this.stateSendCode) {
                    let data = {
                        phone: this.phone,
                        type: 9
                    };
                    this.request.post(API.getCode, data).then(res => {
                        this.stateSendCode = false;
                        this.setTime();
                    }, (res) => {
                        console.log(res);
                    })
                }
            },
            setTime() {
                //    倒计时
                let demo = 70;
                this.codetxt = demo;
                let setTer = setInterval(() => {
                    if (demo > 0) {
                        this.codetxt = demo;
                        demo--;
                    } else {
                        clearInterval(setTer);
                        this.codetxt = '发送验证码';
                        this.stateSendCode = true;
                    }
                }, 1000);
            }
        },
    };

</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
    @import '~assets/main.styl'
    .cell{
        cell()
        .send{
            background-color main-color
            color grayF
            position absolute
            right 0
            width vw(130)
            text-align center
        }
    }
    .sure{
        btn-sta()
    }
</style>
